/*
 * @Description:
 * @Version: 2.0
 * @Author: Mason
 * @Date: 2023-10-14 16:29:55
 * @LastEditors: Mason
 * @LastEditTime: 2023-10-18 22:37:02
 */
package dao

import (
	"context"

	"gitee.com/cy1113/credits/model"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

var User *UserColl

type UserColl struct {
	collName string
	coll     *mongo.Collection
}

func (c *UserColl) Create(u *model.User) (err error) {
	_, err = c.coll.InsertOne(context.Background(), u)
	return nil
}

func (c *UserColl) Find(username string) (u *model.User, err error) {
	cur := c.coll.FindOne(context.Background(), bson.M{"username": username})
	err = cur.Decode(&u)
	return
}

func (c *UserColl) List(page, size int) (u []*model.User, err error) {
	cur, err := c.coll.Find(context.Background(), bson.M{}, options.Find().SetSkip(int64((page-1)*size)).SetLimit(int64(size)))
	err = cur.All(context.Background(), &u)
	return
}
